﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SALIRAPI.Common;
using SALIRAPI.DBContext;
using SALIRAPI.Entities.Map;
using SqlSugar;
using System.Text;

namespace SALIRAPI.Controllers
{
    public class TsUserController : BaseApiController
    {
        private readonly SqlSugarClient isc;
        public TsUserController(SqlSugarClient sugarContent) 
        { 
            isc = sugarContent;
        }
        [HttpGet]
        public async Task<IActionResult> GetAll()//
        {
            var products = isc.Queryable<TsUser>().ToList();
            return ApiResult(true, "查询成功", products);
        }

        [HttpPost]
        public async Task<string> add(string name,decimal phone,string tag)
        {
            var user = new TsUser() { Id = SysHelper.GetGuid(), UserName = name, Phone = phone, Tag = tag };
            var i = isc.Insertable<TsUser>(user).ExecuteCommand();

            return i.ToString();
        }

        [HttpGet]
        public async Task<IActionResult> Login(decimal phone)
        {
            var user = isc.Queryable<TsUser>().Where(x => x.Phone == phone).First();
            var id = SysHelper.GetGuid();
            if (user == null)
            {
                isc.Insertable<TsUser>(new TsUser()
                {
                    Id = id,
                    UserName = SysHelper.GetDefaultNameg(),
                    Phone = phone,
                    Tag = string.Empty
                }).ExecuteCommand();
            }else
            {
                id = user.Id;
            }

            var result =  isc.Queryable<TsUser>().Where(x => x.Id == id).ToList();
            return ApiResult(true, "查询成功", result);
        }
    }
}
